package com.cenwangkeji.resident.ui.activity.special;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.model.bean.Xer36Bean;
import com.cenwangkeji.resident.ui.adapter.SpecialDiseaseDetailAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;
import com.lws.basic.view.TitleLayout;
import com.lws.basic.view.adapter.AdapterIntegerDefault0;
import com.lws.basic.view.adapter.DoubleDefault0Adapter;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

public class Xer36DetailActivity extends BaseActivity {


    @Override
    public int setLayoutId() {
        return R.layout.activity_xer_detail;
    }
    @BindView(R.id.title)
    TitleLayout mTitleLayout;
    @BindView(R.id.rv)
    RecyclerView mRv;
    private SpecialDiseaseDetailAdapter mAdapter;

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        mTitleLayout.setText("3到6岁儿童健康检查记录");
        initRecycelrView();
        getData();
    }

    private void initRecycelrView() {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SpecialDiseaseDetailAdapter(null);
        mRv.setAdapter(mAdapter);
    }


    private void getData() {
        showLoading();
        Observable.create(new ObservableOnSubscribe<Xer36Bean>() {
            @Override
            public void subscribe(ObservableEmitter<Xer36Bean> emitter) throws Exception {
                int id = getIntent().getIntExtra("id", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 3);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkHttpUtils.post( jsonObject.toString(),Constant.xer36_detail);
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, Xer36Bean.class));
            }
        }).compose(RxUtils.<Xer36Bean>io_main()).subscribe(new ObserverOnNext<Xer36Bean>() {
            @Override
            public void onNext(Xer36Bean bean) {
                try {
                    dismissLoading();
                    if (bean.getData() != null) {
                        List<String> list = new ArrayList<>();
                        list.add("年龄：  " + yl(bean.getData().getYl()));
                        list.add("父亲名字：  " + bean.getData().getFxm());
                        list.add("职业：  " + bean.getData().getFzy());
                        list.add("出生日期：  " + bean.getData().getFcsrq());
                        list.add("电话：  " + bean.getData().getFdh());
                        list.add("母亲名字：  " + bean.getData().getMxm());
                        list.add("职业：  " + bean.getData().getMzy());
                        list.add("出生日期：  " + bean.getData().getMcsrq());
                        list.add("电话：  " + bean.getData().getMdh());
                        list.add("体重/cm:  " + tz(bean.getData().getXseTz()));
                        list.add("身长/com:  " + tz(bean.getData().getXseSz()));
                        list.add("体重/身高/cm：  " + tz(bean.getData().getXseSgTz()));
                        list.add("体格发育评估：  " + tgfy(bean.getData().getXseTgfypj()));
                        list.add("听力:  " + (bean.getData().getXseTl() == 1 ? "通过" : "未通过"));
                        list.add("牙颗数:  " + bean.getData().getXseZcs() + "       龋齿数： " + bean.getData().getXseCys());
                        list.add("胸部:  " + yc(bean.getData().getXseXb()));
                        list.add("腹部:  " + yc(bean.getData().getXseFb()));
                        list.add("血红蛋白值：  " + bean.getData().getXseXhdbz() + "g/L");
                        list.add("其他：  " + bean.getData().getXseQt());
                        list.add("发育评估： \n     " +
                                fypg(bean.getData().getFypgList(), bean.getData().getYl()));
                        list.add("两次随访间患病情况:  \n" +
                                lcsf(bean.getData().getLcsfjhzqkList(), bean.getData().getLcsfjhzqkCs2(), bean.getData().getLcsfjhzqkCs3(), bean.getData().getLcsfjhzqkCs4(), bean.getData().getLcsfjhzqkQt()));


                        if (bean.getData().getZzjy() == 2) {// 1无 2有
                            list.add("转诊原因：" + bean.getData().getZzjyYy());
                            list.add("机构科室：" + bean.getData().getZzjyJgjks());

                        }
                        list.add("指导:  " + zd(bean.getData().getZdList(), bean.getData().getZdQt()));
                        list.add("时间：" + bean.getData().getCreateDate());
                        mAdapter.setNewData(list);
                    } else {
                        MyToast.show(bean.getMsg());
                    }
                }catch (Exception e){
                    MyToast.show(e.getMessage());
                    CrashReport.postCatchedException(e);
                }
            }
        });

    }

    //两次随访间患病情况
    private String lcsf(List<String >list,String cs2,String cs3,String cs4,String qt){
        String s ="";
        for (int i= 0; i<list.size();i++) {

            switch (list.get(i)) {
                case "1":
                    s += "肺炎次数" + cs2 + "  ";
                    break;
                case "2":
                    s += "腹泻次数" + cs2 + "   ";
                    break;

                case "3":
                    s += "外伤次数" + cs2 + "   ";
                    break;
                case "4":
                    s += qt;
                    break;


            }
        }
        return s;
    }

    //指导
    private String zd(List<String> list ,String qt){
        String s ="";
        for (int i =0; i<list.size() ;i++) {
            switch (list.get(i)) {
                case "1":
                    s += "合理膳食  ";
                    break;
                case "2":
                    s += "生长发育  ";
                    break;
                case "3":
                    s += "疾病预防  ";
                    break;
                case "4":
                    s += "预防伤害  ";
                    break;
                case "5":
                    s += "口腔保健   ";
                    break;
                case "6":
                    s +=qt;
                    break;


            }
        }
        return s;

    }
    //发育评估
    private String fypg(List<String> list ,int yl){

        String s ="";

            switch (yl){
                case 1:
                    for (int i =0;i<list.size();i++) {
                        switch (list.get(i)) {
                            case "1":
                                s += "不会说自己名字  ";
                                break;
                            case "2":
                                s += "不会模仿画圆  ";
                                break;
                            case "3":
                                s += "不会玩“拿棍当马骑”等假想游戏  ";
                                break;
                            case "4":
                                s += "不会双脚跳 ";
                                break;

                        }
                    }
                    break;
                case 2:
                    for (int i =0;i<list.size();i++) {
                        switch (list.get(i)) {
                            case "1":
                                s += "不会说带形容词句子  ";
                                break;
                            case "2":
                                s += "不会独立穿衣  ";
                                break;
                            case "3":
                                s += "不能按要求等待或轮流  ";
                                break;
                            case "4":
                                s += "不会单脚站立 ";
                                break;

                        }
                    }
                    break;
                case 3:
                    for (int i =0;i<list.size();i++) {
                        switch (list.get(i)) {
                            case "1":
                                s += "不会简单叙述事情经过  ";
                                break;
                            case "2":
                                s += "不知道自己的性别 ";
                                break;
                            case "3":
                                s += "不会用筷子吃饭  ";
                                break;
                            case "4":
                                s += "不会单脚跳 ";
                                break;

                        }
                    }
                    break;
                case 4:
                    for (int i =0;i<list.size();i++) {
                        switch (list.get(i)) {
                            case "1":
                                s += "不会表达自己的感受或想法  ";
                                break;
                            case "2":
                                s += "不会玩角色扮演的集体游戏 ";
                                break;
                            case "3":
                                s += "不会画方形  ";
                                break;
                            case "4":
                                s += "不会奔跑 ";
                                break;

                        }
                    }
                    break;


        }

        return s;

    }

    //异常
    private String yc(int index){
        String s ="";
        switch (index){
            case 1:
                s = "未见异常";
                break;
            case 2:
                s = "异常";
                break;
        }
        return s;
    }


    //体格发育
    private String tgfy(int index){
        String s ="";
        switch (index){
            case 1:
                s = "正常";
                break;
            case 2:
                s = "低体重";
                break;
            case 3:
                s = "消瘦";
                break;
            case 4:
                s = "生长延缓";
                break;
            case 5:
                s = "超重";
                break;

        }

        return  s;

    }

    //体重 上中下
    private String tz(int index){
        String s= "";
        switch (index){
            case 1:
                s = "上";
                break;
            case 2:
                s = "中";
                break;
            case 3:
                s = "下";
                break;


        }
        return s;

    }

    //月龄
    private String yl(int index){
        String s= "";
        switch (index){
            case 1:
                s = "三岁";
                break;
            case 2:
                s = "四岁";
                break;
            case 3:
                s = "五岁";
                break;
            case 4:
                s = "六岁";
                break;

        }
        return s;
    }


}
